import 'package:flutter/material.dart';
import 'package:gif/gif.dart';
import '../../models/order_detail_model.dart';
import '../../l10n/app_localizations.dart';

class LotteryDrawView extends StatefulWidget {
  final DetailActivity model;

  const LotteryDrawView({super.key, required this.model});

  @override
  State<LotteryDrawView> createState() => _LotteryDrawViewState();
}

class _LotteryDrawViewState extends State<LotteryDrawView> with TickerProviderStateMixin {
  bool showGif = true;
  late GifController _gifController;

  @override
  void initState() {
    super.initState();
    _gifController = GifController(vsync: this);

    // 启动动画后 1 秒切换为结果图
    Future.delayed(const Duration(seconds: 1), () {
      if (mounted) {
        setState(() {
          showGif = false;
        });
      }
    });
  }

  @override
  void dispose() {
    _gifController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final model = widget.model;

    Widget priceWidget;
    if (model.type == '0') {
      priceWidget = RichText(
        text: TextSpan(
          children: [
            TextSpan(
              text: model.symbol,
              style: const TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
            TextSpan(
              text: model.parvalue.toStringAsFixed(2),
              style: const TextStyle(
                fontSize: 35,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
          ],
        ),
      );
    } else {
      final float1 = model.parvalue * 10;
      priceWidget = RichText(
        text: TextSpan(
          children: [
            TextSpan(
              text: float1.toStringAsFixed(1),
              style: const TextStyle(
                fontSize: 35,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
            const TextSpan(
              text: '折',
              style: TextStyle(
                fontSize: 17,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
          ],
        ),
      );
    }

    return GestureDetector(
      onTap: () {
        Navigator.of(context).pop(); // 模拟 removeFromSuperview
      },
      child: Material(
        color: Colors.black.withValues(alpha: 0.5),
        child: Center(
          child: showGif
              ? Gif(
                  controller: _gifController,
                  fps: 10,
                  autostart: Autostart.once,
                  image: const AssetImage('assets/images/chou.gif'),
                  // width: 200,
                  height: 200,
                  fit: BoxFit.cover,
                )
              : Stack(
                  alignment: Alignment.center,
                  children: [
                    Image.asset(
                      'assets/images/抽奖弹窗.png',
                      width: 350,
                      height: 361,
                      fit: BoxFit.contain,
                    ),
                    Positioned(
                      top: 60,
                      child: Text(
                        AppLocalizations.of(context)!.lotteryWin,
                        style: const TextStyle(
                          fontSize: 25,
                          fontWeight: FontWeight.bold,
                          color: Color(0xFF802709),
                        ),
                      ),
                    ),
                    Positioned(
                      top: 102,
                      child: Text(
                        AppLocalizations.of(context)!.lotteryAutoSave,
                        style: const TextStyle(
                          fontSize: 13,
                          color: Color(0xFF802709),
                        ),
                      ),
                    ),
                    Positioned(
                      top: 150,
                      child: Text(
                        model.name,
                        style: const TextStyle(fontSize: 18, color: Colors.white),
                      ),
                    ),
                    Positioned(
                      top: 175,
                      child: priceWidget,
                    ),
                    Positioned(
                      top: 215,
                      child: Text(
                        '满${model.symbol}${model.cill.toStringAsFixed(0)}可用',
                        style: const TextStyle(fontSize: 14, color: Colors.white),
                      ),
                    ),
                    Positioned(
                      top: 260,
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor: Colors.transparent,
                          foregroundColor: Colors.white,
                          elevation: 0,
                          textStyle: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                        ),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                        child: Text(AppLocalizations.of(context)!.lotteryGotIt),
                      ),
                    )
                  ],
                ),
        ),
      ),
    );
  }
}
